<template>
  <div class="recommend-goods" v-loading.fullscreen.lock="loading">
    <page-main :pagination="pagination" @search-data="searchData" @page-change="pageChange">
      <template slot="search">
        <search-form :search="search"></search-form>
      </template>
      <template slot="append">
        <el-button type="primary" size="medium" @click="addGoods">新增</el-button>
      </template>
      <template slot="table">
        <el-table fit stripe border highlight-current-row :data="goodsList" height="100%">
          <el-table-column label="活动类型" min-width="120px" header-align="center">
            <template slot-scope="scope">
              <span>{{i18n.getMsg(`goods.recommendGoods.type.${scope.row.activityTypeId}`)}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="branchName" label="所属总店" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="partnerName" label="所属分店" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="goodsName" label="活动商品" min-width="120px" header-align="center"></el-table-column>
          <el-table-column :resizable="false" label="开始时间" width="160px" align="center">
            <template slot-scope="scope">
              <span>
                {{$moment(scope.row.startTime).format('YYYY-MM-DD HH:mm:ss')}}
              </span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="结束时间" width="160px" align="center">
            <template slot-scope="scope">
              <span>
                {{$moment(scope.row.endTime).format('YYYY-MM-DD HH:mm:ss')}}
              </span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="状态" width="80px" align="center">
            <template slot-scope="scope">
              <span>{{i18n.getMsg(`goods.recommendGoods.status.${scope.row.status}`)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="操作" width="80px" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="updateGoods(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </page-main>

    <el-dialog :title="dialogTitle[dialogType]" custom-class="sys-dialog" :visible.sync="openDialog" :close-on-click-modal="false" width="450px" top="10vh" @closed="dialogClosed">
      <div style=":max-height:calc(80vh - 54px);height:370px;overflow:auto">
        <recommend-goods-form v-if="openDialog" :goods="selectGoods" :type="dialogType" :dropdown-option="dropdownOption" @save-success="saveSuccess"></recommend-goods-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import RecommendGoodsApi from 'services/recommendGoodsApi';

  import PageMain from 'commonComponents/pageMain';
  import SearchForm from './searchForm';
  import RecommendGoodsForm from './recommendGoodsForm';

  import Pagination from 'models/pagination';
  import RecommendGoosSearch from 'models/goods/recommendGoosSearch';

  export default {
    data() {
      return {
        loading: false,
        search: new RecommendGoosSearch(),
        pagination: new Pagination(),
        goodsList: [],
        dropdownOption:{},
        openDialog: false,
        dialogType: '',
        selectGoods: null,
        dialogTitle: {
          add: "新增商品推荐",
          update: "修改商品推荐"
        }
      };
    },

    created(){
      this.setGoodsList(true);
    },

    methods:{
      setGoodsList(isSearch = false){
        let search = this.search;
        if (!isSearch) {
          search.resetOldData();
        }

        let pagination = this.pagination;
        this.loading = true;
        RecommendGoodsApi.getRecommendGoodsList(search, pagination).then(goodsList=>{
          if (isSearch) {
            search.setOldData();
          }

          if (goodsList.length === 0 && pagination.currentPage > 1) {
            this.$nextTick(() => {
              this.setGoodsList(isSearch);
            });
            return;
          }

          this.goodsList = goodsList;
          this.loading = false;
        }).catch(() => {
          this.loading = false;
        });
      },

      searchData(){
        this.pagination.currentPage = 1;
        this.setGoodsList(true);
      },

      pageChange(){
        this.setGoodsList();
      },

      addGoods(){
        this.dialogType = 'add';
        this.openDialog = true;
      },

      updateGoods(goods){
        this.dialogType = 'update';
        this.selectGoods = goods;
        this.openDialog = true;
      },

      saveSuccess(){
        this.openDialog = false;
        this.setGoodsList();
      },

      dialogClosed(){
        this.dialogType = '';
        this.selectGoods = null;
      }
    },

    components:{
      PageMain,
      SearchForm,
      RecommendGoodsForm
    }
  };

</script>

<style lang="scss" scoped>
  .recommend-goods {
    height: 100%;
  }

</style>
